package com.example.qxfw.dataBaseManager.nyzjk.controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.ny.CropGrowdateIndexlib;
import com.example.qxfw.common.entity.ny.CropWeatherDistIndexlib;
import com.example.qxfw.dataBaseManager.nyzjk.service.QxzhzbkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/************************************
 *
 * @author : HuangDeYin Jolly
 * @since : JDK 11
 * @Date : 2023/7/6  星期四   9:59
 * @description : 此类用来  气象灾害指标库
 ***************************************/
@Slf4j
@Api(tags = "数据库管理-农业专家库-气象灾害指标库")
@RestController
@CrossOrigin
@RequestMapping("/QxzhzbkController")
public class QxzhzbkController {

    @Autowired
    private QxzhzbkService qxzhzbkService;

    @ApiOperation("查询cropname")
    @GetMapping("selectDistinct")
    public SysResult selectDistinct() {
        List<CropGrowdateIndexlib> cropGrowdateIndexlibs = this.qxzhzbkService.selectDistinct("cropname", null);
        return SysResult.success(cropGrowdateIndexlibs);
    }

    @ApiOperation("查询所哟")
    @GetMapping("selectAll")
    public SysResult selectAll() {
        List<CropWeatherDistIndexlib> cropWeatherDistIndexlibs = this.qxzhzbkService.selectAll();
        return SysResult.success(cropWeatherDistIndexlibs);
    }

    @ApiOperation("条件查询")
    @PostMapping("querySelect")
    public SysResult querySelect(@RequestBody CropWeatherDistIndexlib cropWeatherDistIndexlib) {
        List<CropWeatherDistIndexlib> cropWeatherDistIndexlibs = this.qxzhzbkService.querySelect(cropWeatherDistIndexlib);
        return SysResult.success(cropWeatherDistIndexlibs);

    }

    @ApiOperation("添加")
    @PostMapping("saveCropWeather")
    public SysResult saveCropWeather(@RequestBody CropWeatherDistIndexlib cropWeatherDistIndexlib) {
        List<CropWeatherDistIndexlib> cropWeatherDistIndexlibs = this.qxzhzbkService.querySelect(cropWeatherDistIndexlib);
        if (cropWeatherDistIndexlibs.size() > 0) {
            return SysResult.fail("存在相同的灾害类型");
        } else {
            Integer integer = this.qxzhzbkService.saveCropWeatherDistIndexLib(cropWeatherDistIndexlib);
            if (integer > 0) {
                return SysResult.success();
            } else {
                return SysResult.fail();
            }
        }
    }

    @ApiOperation("删除")
    @GetMapping("deleteCropWeather")
    public SysResult deleteCropWeather(String id) {
        CropWeatherDistIndexlib cropWeatherDistIndexlib = new CropWeatherDistIndexlib();
        cropWeatherDistIndexlib.setId(id);
        Integer integer = this.qxzhzbkService.deleteCropWeatherDistIndexLib(cropWeatherDistIndexlib);
        if (integer > 0) {
            return SysResult.success();
        } else {
            return SysResult.fail();
        }
    }

    @ApiOperation("更新")
    @PostMapping("updateCropWeather")
    public SysResult updateCropWeather(@RequestBody CropWeatherDistIndexlib cropWeatherDistIndexlib) {
        Integer integer = this.qxzhzbkService.updateCropWeatherDistIndexLib(cropWeatherDistIndexlib);
        if (integer > 0) {
            return SysResult.success();
        } else {
            return SysResult.fail();
        }
    }

    @ApiOperation("查询GrowdateName")
    @GetMapping("selectDistinctGrowdateName")
    public SysResult selectDistinctGrowdateName(String cropname) {
        List<CropGrowdateIndexlib> cropGrowdateIndexlibs = this.qxzhzbkService.selectDistinct("growdatename", cropname);
        return SysResult.success(cropGrowdateIndexlibs);
    }


}
